﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealState.BusinessObjects;
using RealState.BLL;
using log4net;
using RealState.Utils;

namespace RealState.W.uc
{
    public partial class ucListNews : System.Web.UI.UserControl
    {
        private News newsBO;
        private NewsBLL newsBLL;
        private ILog log = LogManager.GetLogger("ucListnews");
        private List<News> listNews;
        private short pageSize = 2;
        private static int rowCount;

        protected void Page_Init(object sender, System.EventArgs e)
        {
            //ucDataNavigator.CurrentPage = 1;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            newsBLL = new NewsBLL();
            newsBO = new News();
            try
            {
                //listNews = newsBLL.GetNewss().FindAll(delegate(News n)
                //                        {
                //                            return n.RowNum > ((ucDataNavigator.CurrentPage-1)*pageSize) && n.RowNum < (ucDataNavigator.CurrentPage * pageSize + 1);
                //                        }
                //                        );
                //rowCount = newsBLL.GetNewss().ToList().Count; ;
                //Repeater1.DataSource = listNews;
                //PreparePaging(ucDataNavigator.CurrentPage);
                //Repeater1.DataBind();
                Repeater1.DataSource = newsBLL.GetNewss();
                Repeater1.DataBind();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            
        }
        //#region Navigator Button Controller

        //private void PreparePaging(int pageIndex)
        //{

        //    int numOfPage;

        //    if (rowCount == 0)
        //    {
        //        numOfPage = 0;
        //    }
        //    else if (rowCount <= pageSize)
        //    {
        //        numOfPage = 1;
        //    }
        //    else
        //    {
        //        if (rowCount % pageSize == 0)
        //        {
        //            numOfPage = rowCount / pageSize;
        //        }
        //        else
        //        {
        //            numOfPage = rowCount / pageSize + 1;
        //        }
        //    }

        //    //Khởi tạo lại PageController
        //    if (numOfPage == 0)
        //        ucDataNavigator.RefreshControl(0, numOfPage);
        //    else if (pageIndex <= numOfPage)
        //        ucDataNavigator.RefreshControl(pageIndex, numOfPage);
        //    else
        //        ucDataNavigator.RefreshControl(1, numOfPage);
        //    //Thiết lập Enable và Disable các nút di chuyển trang
        //    EnableDisableButtons(numOfPage);

        //}

        //protected void PageChanged(object sender, DataNavigatorEventArgs e)
        //{
        //    ucDataNavigator.CurrentPage = e.CurrentPage;
        //    LoadData();
        //    EnableDisableButtons(e.TotalPages);
        //}

        //protected void FirstPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage > 1)
        //    {
        //        ucDataNavigator.CurrentPage = 1;
        //        // Get the data for the DataGrid.
        //        LoadData();
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void PreviousPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage > 1)
        //    {
        //        ucDataNavigator.CurrentPage--;
        //        // Get the data for the DataGrid.
        //        LoadData();
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void NextPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage <= e.TotalPages)
        //    {
        //        ucDataNavigator.CurrentPage++;
        //        // Get the data for the DataGrid.
        //        LoadData();
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void LastPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage <= e.TotalPages)
        //    {
        //        ucDataNavigator.CurrentPage = e.TotalPages;
        //        // Get the data for the DataGrid.
        //        LoadData();
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //private void EnableDisableButtons(int iTotalPages)
        //{
        //    // Should we disable the first link?
        //    ucDataNavigator.FirstButtonEnabled = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? false : true;
        //   // ucDataNavigator.FirstButtonImageUrl = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? "~/images/first1.png" : "~/images/first1.png";
        //    // Should we disable the previous link?
        //    ucDataNavigator.PreviousButtonEnabled = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? false : true;
        //   // ucDataNavigator.PreviousButtonImageUrl = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? "~/images/prev1.png" : "~/images/prev1.png";
        //    // Should we enable the next link?
        //    ucDataNavigator.NextButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
        //   // ucDataNavigator.NextButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "~/images/next1.png" : "~/images/next1.png";
        //    // Should we enable the last link?
        //    ucDataNavigator.LastButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
        //    //ucDataNavigator.LastButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "~/images/next1.png" : "~/images/last1.png";
        //}

        //override protected void OnInit(EventArgs e)
        //{
        //    InitializeComponent();
        //    base.OnInit(e);
        //}

        //private void InitializeComponent()
        //{
        //    //this.lstdivUserRef.SelectedIndexChanged += new EventHandler(lstdivUserRef_SelectedIndexChanged);
        //    this.ucDataNavigator.FirstPage += new FirstPageEventHandler(this.FirstPage);
        //    this.ucDataNavigator.PreviousPage += new PreviousPageEventHandler(this.PreviousPage);
        //    this.ucDataNavigator.NextPage += new NextPageEventHandler(this.NextPage);
        //    this.ucDataNavigator.LastPage += new LastPageEventHandler(this.LastPage);
        //    this.ucDataNavigator.PageChanged += new PageChangedEventHandler(this.PageChanged);
        //    this.Init += new EventHandler(this.Page_Init);
        //}

        //#endregion
    }
}